package com.datas.utils.listener;

import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * Created by jige0727 on 2017/3/12.
 */
@Component
public class DataInitListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
//        LoggerFactory.getLogger(getClass()).info("收到事件 " + applicationEvent + " -> " + applicationEvent.getSource());
        if (applicationEvent instanceof ContextRefreshedEvent) {
            ContextRefreshedEvent event = (ContextRefreshedEvent) applicationEvent;
            if (event.getApplicationContext().getParent() == null) {
                LoggerFactory.getLogger(getClass()).info("初始化 启动系统");
            }
        }
        if (applicationEvent instanceof ContextClosedEvent) {
            ContextClosedEvent event = (ContextClosedEvent) applicationEvent;
            if (event.getApplicationContext().getParent() == null) {
                LoggerFactory.getLogger(getClass()).info("销毁事件 关闭系统");
            }
        }
    }

}
